package com.ponhu.ea2.stockcenter.provider.utils;

import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.function.BinaryOperator;
import java.util.function.Function;
import java.util.stream.Collectors;

public class CommonUtils {

    /**
     * 将 List 统计聚合转变为 Map
     *
     * @param tList      原始集合
     * @param classifier 集合分组函数
     * @param mapper     集合元素映射函数
     * @param identity   聚合初始值
     * @param op         集合分组内聚合函数
     * @param <T>        集合元素泛型
     * @param <K>        Map-key泛型
     * @param <V>        Map-value泛型
     * @return
     */
    public static <T, K, V> Map<K, V> ListStatisticsToMap(List<T> tList,
                                                          Function<? super T, ? extends K> classifier,
                                                          Function<? super T, ? extends V> mapper,
                                                          V identity, BinaryOperator<V> op) {
        return tList.parallelStream().collect(Collectors.groupingBy(classifier))
                .entrySet().parallelStream().collect(Collectors.toMap(Map.Entry::getKey, entry ->
                        entry.getValue().parallelStream().map(mapper)
                                .collect(Collectors.reducing(identity, op))));
    }

    public static String getUUID() {
        return UUID.randomUUID().toString().replaceAll("-", "");
    }

    public static boolean isEmpty(String str) {
        return StringUtils.isEmpty(str) || "NULL".equalsIgnoreCase(str) || "null".equalsIgnoreCase(str);
    }

    public static boolean isNotEmpty(String str) {
        return StringUtils.isNotEmpty(str) && !"NULL".equalsIgnoreCase(str) && !"null".equalsIgnoreCase(str);
    }

    public static boolean isEmptyNum(Integer integer) {
        return ObjectUtils.isEmpty(integer) || integer == 0;
    }

}
